﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETADTools.Lib
{
  public static class FieldListFormExtension
  {
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToSqlObjectResource(this FieldList fieldList)
    {
      return string.Join(" \n", fieldList.Select(t => t.ToSqlObjectResource()));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToSqlFieldGroup(this FieldList fieldList)
    {
      return string.Join(" \n\n", fieldList.Select(t => t.ToSqlFieldGroup()));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToAspCreateControl(this FieldList fieldList)
    {
      return string.Join("\n  ", fieldList.Select(t => t.ToAspCreateControl()));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToAspSaveParameter(this FieldList fieldList)
    {
      return string.Join("\n      ", fieldList.Select(t => t.ToAspSaveParameter()));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToAspRenderLayout(this FieldList fieldList)
    {
      StringBuilder result = new StringBuilder();

      for (int i = 0; i < fieldList.Count; ++i)
      {
        // Render the <tr> tag each 2 rows
        if (i % 2 == 0)
        {
          result.AppendFormat("\n    <tr>\n      ");
          result.AppendFormat(fieldList[i].ToAspRenderLayout());
          //result.AppendFormat("\n      ");

          if (i == fieldList.Count - 1)
          {
            result.AppendFormat("\n    </tr>");
          }
        }
        else
        {
          result.AppendFormat("\n      ");
          result.AppendFormat(fieldList[i].ToAspRenderLayout());
          result.AppendFormat("\n    </tr>");
        }
      }
      return result.ToString();
    }
  }
}
